import {
  getWeekAndMonthCount,
  getCountBySource,
  getHandleCount,
  getCountByTypeAndYear,
} from '@/services/problem';

export default {
  nampspace: 'problem',

  state: {
    calenderStatistic: {},
    sourceStatistic: {},
    handleStatistic: {},
    typeCountStatistic: {},
  },

  effects: {
    *fetchCalenderStatistic({ payload }, { call, put }) {
      const response = yield call(getWeekAndMonthCount, payload);

      yield put({
        type: 'getCalenderStatistic',
        payload: response,
      });
    },

    *fetchSourceStatistic({}, { call, put }) {
      const response = yield call(getCountBySource);

      yield put({
        type: 'getSourceStatistic',
        payload: response,
      });
    },

    *fetchHandleStatistic({ callback }, { call, put }) {
      const response = yield call(getHandleCount);

      yield put({
        type: 'getHandleCount',
        payload: response,
      });

      if (response) callback(response);
    },

    *fetchTypeCountStatistic({ callback }, { call, put }) {
      const response = yield call(getCountByTypeAndYear);

      yield put({
        type: 'getTypeCountStatistic',
        payload: response,
      });

      if (response) callback(response);
    },
  },

  reducers: {
    getCalenderStatistic(state, { payload }) {
      return {
        ...state,
        calenderStatistic: payload,
      };
    },

    getSourceStatistic(state, { payload }) {
      return {
        ...state,
        sourceStatistic: payload,
      };
    },

    getHandleCount(state, { payload }) {
      return {
        ...state,
        handleStatistic: payload,
      };
    },

    getTypeCountStatistic(state, { payload }) {
      return {
        ...state,
        typeCountStatistic: payload,
      };
    },
  },
};
